Imports C4F.DevKit.DXSoundRecorder

Partial Public Class Window1
    Inherits System.Windows.Window

    Private Const DefaultRate As SampleRate = SampleRate.Rate44KHz
    Private Const DefaultSize As SampleSize = SampleSize.Bits16
    Private Const DefaultDeviceType As SoundDeviceType = SoundDeviceType.Default

    Private _recorder As SoundRecorder = Nothing

    Private _samplesPerSecond As SampleRate
    Private _bitsPerSample As SampleSize
    Private _deviceType As SoundDeviceType

    Private _filename As String = String.Empty
    Private _path As String = String.Empty
    Private _recording As Boolean = False

    ' Initialize the window and set the default settings.
    Public Sub New()
        InitializeComponent()

        Me.buttonStartStop.IsEnabled = False

        Me.textBoxFileInfo.Text = "Select a file to record to..."
        Me.textBoxRecordInfo.Text = String.Empty

        Me.comboBoxRate.ItemsSource = System.Enum.GetNames(GetType(SampleRate))
        Me.comboBoxRate.SelectedIndex = 0
        AddHandler comboBoxRate.SelectionChanged, AddressOf comboBoxRate_SelectionChanged

        Me.comboBoxSize.ItemsSource = System.Enum.GetNames(GetType(SampleSize))
        Me.comboBoxSize.SelectedIndex = 0
        AddHandler comboBoxSize.SelectionChanged, AddressOf comboBoxSize_SelectionChanged

        Me.comboBoxDevice.ItemsSource = System.Enum.GetNames(GetType(SoundDeviceType))
        Me.comboBoxDevice.SelectedIndex = 0
        AddHandler comboBoxDevice.SelectionChanged, AddressOf comboBoxDevice_SelectionChanged

        SetDefaultSettings()
    End Sub

    Private Sub SetDefaultSettings()
        If (Not System.Enum.IsDefined(GetType(SampleRate), Me._samplesPerSecond)) Then
            Me.comboBoxRate.SelectedIndex = Me.comboBoxRate.Items.IndexOf(DefaultRate.ToString())
        End If
        If (Not System.Enum.IsDefined(GetType(SampleSize), Me._bitsPerSample)) Then
            Me.comboBoxSize.SelectedIndex = Me.comboBoxSize.Items.IndexOf(DefaultSize.ToString())
        End If
        If (Not System.Enum.IsDefined(GetType(SoundDeviceType), Me._deviceType)) Then
            Me.comboBoxDevice.SelectedIndex = Me.comboBoxDevice.Items.IndexOf(DefaultDeviceType.ToString())
        End If
    End Sub

    Private Sub comboBoxRate_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim selection As String = TryCast(e.AddedItems(0), String)

        Me._samplesPerSecond = CType(System.Enum.Parse(GetType(SampleRate), selection), SampleRate)
    End Sub

    Private Sub comboBoxSize_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim selection As String = TryCast(e.AddedItems(0), String)

        Me._bitsPerSample = CType(System.Enum.Parse(GetType(SampleSize), selection), SampleSize)
    End Sub

    Private Sub comboBoxDevice_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim selection As String = TryCast(e.AddedItems(0), String)

        Me._deviceType = CType(System.Enum.Parse(GetType(SoundDeviceType), selection), SoundDeviceType)
    End Sub

    Private Sub OnSave(ByVal sender As Object, ByVal args As RoutedEventArgs)
        OnCreateSoundFile()

        Me.buttonStartStop.IsEnabled = True
    End Sub

    Private Sub OnStartStop(ByVal sender As Object, ByVal args As RoutedEventArgs)
        If (Not Me._recording) Then
            ' Start recording...
            Try
                Me._recorder = New SoundRecorder(Me._deviceType, Me._samplesPerSecond, Me._bitsPerSample, 1)
            Catch ex As Exception
                System.Windows.MessageBox.Show("Could not create sound recorder: " & ex.Message)
                Return
            End Try

            Me._recorder.Start(_filename)

            Me._recording = True
            Me.buttonStartStop.Content = "Stop"
            Dim recordBaseMsg As String = "Recording at {0}KHz and {1}bits/sample using {2} device... click Stop when done."
            Dim recordMsg As String = String.Format(recordBaseMsg, CInt(Fix(Me._samplesPerSecond)), CShort(Fix(Me._bitsPerSample)), Me._deviceType)
            Me.textBoxRecordInfo.Text = recordMsg
        Else
            If Me._recording Then
                ' Stop recording...
                Me._recorder.Stop()

                Me._recording = False
                Me.buttonStartStop.Content = "Start"
                Me.textBoxRecordInfo.Text = "Finished recording."

                Me._recorder.Dispose()
            End If
        End If
    End Sub

    Private Sub OnCreateSoundFile()
        Dim sfd As Forms.SaveFileDialog = New Forms.SaveFileDialog()
        Dim result As Forms.DialogResult

        If String.Empty = Me._path Then
            Me._path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        End If

        sfd.DefaultExt = ".wav"
        sfd.Filter = "Wave Files|*.wav|All Files|*.*"
        sfd.FileName = Me._filename
        sfd.InitialDirectory = Me._path

        Me.textBoxFileInfo.Text = "Saving file..."

        ' Display the OpenFileName dialog. Then, try to load the specified file
        result = sfd.ShowDialog()

        If result = System.Windows.Forms.DialogResult.Abort OrElse result = System.Windows.Forms.DialogResult.Cancel Then
            Me.textBoxFileInfo.Text = "Save aborted."
            Return
        End If
        Me._filename = sfd.FileName

        ' Update the UI controls to show the sound as the file is loaded
        Me.textBoxFileInfo.Text = "Saving to " & Me._filename
    End Sub

End Class
